package commons.base.http.example;

import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URL;
import java.util.List;

import commons.base.http.HttpBuilder;
import commons.base.http.SimpleHttpClient;
import commons.base.http.jdk.Form;

public class HttpTest4 {

	public static void main(String[] args)throws Exception {
//		t1();
//		System.err.println(new String(Base64.getDecoder().decode("d2FuZ3hoOnd4aDIwMDA=".getBytes("UTF-8"))));
		t4();
	}
	
	public static void t1()throws Exception{
		String r = SimpleHttpClient.newGet("http://www.cnblogs.com/comsokey/p/PhpCurlFiddler.html")
				.setProxy("127.0.0.1", 8888, null, null)
				.execute().getBody();
		System.err.println(r);
	}
	
	public static void t2()throws Exception{
		// 同时支持代理 HTTP/HTTPS 请求
//		System.setProperty("proxyHost","127.0.0.1");
//		System.setProperty("proxyPort","8888");
		
		Proxy proxy =new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1",8888));
		URL obj =new URL("http://www.cnblogs.com/comsokey/p/PhpCurlFiddler.html"); 
		HttpURLConnection con = (HttpURLConnection) obj.openConnection(proxy);
//		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		System.err.println(con.getResponseCode());
	}
	
	public static void t3()throws Exception{
		String r = HttpBuilder.newGet("http://www.cnblogs.com/comsokey/p/PhpCurlFiddler.html")
						.addFormField("a", "1")
						.addFormField("b", "2")
						.execute()
						.getBodyAsText();
		System.err.println(r);
	}
	public static void t4()throws Exception{
		String r = HttpBuilder.newGet("http://www.cnblogs.com/comsokey/p/PhpCurlFiddler.html")
						.addFormField("a", "1")
						.addFormField("b", "2").apply().getRequest().getUri();
				
		System.err.println(r);
	}
	
	private static Proxy findProxy(URI uri)
	{
	     try
	     {
	          ProxySelector selector = ProxySelector.getDefault();
	          List<Proxy> proxyList = selector.select(uri);
	          if (proxyList.size() > 1)
	               return proxyList.get(0);
	     }
	     catch (IllegalArgumentException e)
	     {
	     }
	     return Proxy.NO_PROXY;
	}
}
